package com.fsquare.component.captcha;

import javax.servlet.http.HttpServletRequest;

import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaFactory;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;


@Component("blReCaptchaProcessor")
public class ReCaptchaProcessor{
	
	private static final Log LOG = LogFactory.getLog(ReCaptchaProcessor.class);
	
	
	@Value("${recaptcha.private.key}")
    private String recaptchaPrivateKey;
    
    @Value("${recaptcha.public.key}")
    private String recaptchaPublicKey;
    
    @Value("${domain.name}")
    private String domainName;
    
    public String create(){
    	ReCaptcha reCaptcha = ReCaptchaFactory.newReCaptcha(recaptchaPublicKey, recaptchaPrivateKey, false);
		String captcha = reCaptcha.createRecaptchaHtml(null, null);
		return captcha;
    }
	

    public boolean test(HttpServletRequest request) {    
    	String remoteAddr = request.getRemoteAddr();
        String response = request.getParameter("recaptcha_response_field");
		String challenge = request.getParameter("recaptcha_challenge_field");
    	
    	
    	LOG.info("recaptchaPublicKey: "+recaptchaPublicKey);
    	LOG.info("recaptchaPrivateKey: "+recaptchaPrivateKey);
    	LOG.info("remoteAddr: "+remoteAddr);
    	LOG.info("recaptcha_response_field: "+response);
    	LOG.info("recaptcha_challenge_field: "+challenge);
        
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey(recaptchaPrivateKey);

        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, response);
      
        LOG.info("reCaptchaResponse: "+reCaptchaResponse.isValid());
        
        return reCaptchaResponse.isValid();
    }
    
    public boolean test(HttpServletRequest request, Errors errors){
    	String remoteAddr = request.getRemoteAddr();
        String response = request.getParameter("recaptcha_response_field");
		String challenge = request.getParameter("recaptcha_challenge_field");
    	
    	
    	LOG.info("recaptchaPublicKey: "+recaptchaPublicKey);
    	LOG.info("recaptchaPrivateKey: "+recaptchaPrivateKey);
    	LOG.info("remoteAddr: "+remoteAddr);
    	LOG.info("recaptcha_response_field: "+response);
    	LOG.info("recaptcha_challenge_field: "+challenge);
    	if(StringUtils.isBlank(response))
    		ValidationUtils.rejectIfEmpty(errors, "captchaForm.recaptcha_response_field", "captcha.required");
        
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey(recaptchaPrivateKey);

        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, response);
      
        LOG.info("reCaptchaResponse: "+reCaptchaResponse.isValid());
        
    	boolean validCaptcha = reCaptchaResponse.isValid();
    	if(!validCaptcha)
			ValidationUtils.rejectIfEmpty(errors, "captchaForm.recaptcha_response_field", "captcha.invalid");
    	
    	return validCaptcha;
    }
	
}
